/* internal/js/menu-item/form.js */

function resetFields () {
	document.getElementById('menuItemID').value = '';
	document.getElementById('foodItemID').value = '';
	document.getElementById('foodItemInMIID').value = '';
	document.getElementById('number').value = '';
	document.getElementById('name').value = '';
	document.getElementById('price').value = '';
	document.getElementById('foodItemInMIList').innerHTML = '';
	document.getElementById('foodItemType').value = '';
	document.getElementById('foodItemName').value = '';
	document.getElementById('menu').selectedIndex = '0';
	
	hideFieldMessage('numberMessage');
	hideFieldMessage('nameMessage');
	hideFieldMessage('priceMessage');
	
	selectResultRow(0);
	selectFoodItemInMIRow(0);
	selectFoodItemRow(0);
}

function loadPage () {
	listFoodItem();
	listMenus();
}


/* Build Menu Item Result Table */

function startResultsTable () {
	var html = '<table class="searchResults">' +
	           '<tr>' +
	           '<th> Item No. </th>' +
	           '<th> Name </th>' +
	           '<th> Price </th>' +
	           '</tr>';
	return html;
}


function buildTable (menuitems) {
	var html = startResultsTable();
	var count = menuitems.childNodes.length;
	var ids = new Array(count);

	for (var loop = 0; loop < count; loop++) {
		var menuitem = menuitems.childNodes[loop];
		var id = menuitem.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		
		ids[loop] = id;
		
		if (menuitem.getElementsByTagName('number')[0].childNodes.length == 0)
			var number = '';
		else
			number = menuitem.getElementsByTagName('number')[0].childNodes[0].nodeValue;
		
		if (menuitem.getElementsByTagName('menu-item-name')[0].childNodes.length == 0)
			var name = '';
		else
			name = menuitem.getElementsByTagName('menu-item-name')[0].childNodes[0].nodeValue;
		
		if (menuitem.getElementsByTagName('price')[0].childNodes.length == 0)
			var price = '';
		else
			price = menuitem.getElementsByTagName('price')[0].childNodes[0].nodeValue;
		
		if (menuitem.getElementsByTagName('menu-name')[0].childNodes.length == 0)
			var menu = '';
		else
			menu = menuitem.getElementsByTagName('menu-name')[0].childNodes[0].nodeValue;
		
		html += addResultRow(id, number, name, price, menu);
	}
	
	html += endResultsTable(ids);
	return html;
}

function addResultRow (id, number, name, price, menu) {
	var	html =
		'<form id="listMenuItemForm' + id + '">' +
		'<input id="number" value="' + number + '" type="hidden" />' +
		'<input id="name" value="' + name + '" type="hidden" />' +
		'<input id="price" value="' + price + '" type="hidden" />' +
		'<input id="menu" value="' + menu + '" type="hidden" />' +
		'</form>' +
		'<tr id="listMenuItemRow' + id + '" class="searchResultsRow" onclick="showResult(\'' + id + '\'),' +
		'listFoodItemInMI(\'' + id + '\')">' +
		'<td align="right">' + number + '</td>' +
		'<td align="center">' + name + '</td>' +
		'<td align="right">' + price + '</td>';
	return html;
}

function endResultsTable (ids) {
	var html =
		'<input id="resultsIds" value="' + ids.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}



function selectResultRow (id) {
	if (document.getElementById('resultsIds') != null) {
		var ids = document.getElementById('resultsIds').value.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listMenuItemRow' + ids[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listMenuItemRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}

function showResult (id) {
	selectResultRow(id);
	var frm = document.getElementById('listMenuItemForm' + id);

	document.getElementById('menuItemID').value = id;
	document.getElementById('number').value = frm.number.value;
	document.getElementById('name').value = frm.name.value;
	document.getElementById('price').value = frm.price.value;
	
	var menuList = document.getElementById('menu');
	var menu = frm.menu.value;
	for (var loop = 0; loop < menuList.length; loop++) {
		if (menuList.options[loop].innerHTML == menu) {
			menuList.selectedIndex = loop;
			break;
		}
	}
}

/* END Build Menu Item Result Table */

/* Build Food Item Table */

function buildFoodItemTable(type, fooditems) {
	var html = '<table class="searchResults">' +
    '<tr>' +
    '<th> Name </th>' +
    '<th> Type </th>' +
    '<th> Cost </th>';
	if (type == 'FoodItem')
		html += '<th> Availability </th>';
	html += '</tr>';
	var count = fooditems.childNodes.length;
	var ids = new Array(count);
	var counter = 0;
	for (var loop = 0; loop < count; loop++) {
		var fooditem = fooditems.childNodes[loop];
		var id = fooditem.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		
		if (type == 'FoodItem') {
			// get the show option if it's menu item
			var showAll = document.showOpForm.showOp[0].checked;
			var available = fooditem.getElementsByTagName('available')[0].childNodes[0].nodeValue;

			if (showAll || available) {
				ids[counter++] = id;
				// Get all the information from fooditem xml
				if (fooditem.getElementsByTagName('name')[0].childNodes.length > 0)
					var name = fooditem.getElementsByTagName('name')[0].childNodes[0].nodeValue;
				else
					name = '';
				
				if (fooditem.getElementsByTagName('description')[0].childNodes.length > 0)
					var description = fooditem.getElementsByTagName('description')[0].childNodes[0].nodeValue;
				else
					description = '';
				
				if (fooditem.getElementsByTagName('cost')[0].childNodes.length > 0)
					var cost = fooditem.getElementsByTagName('cost')[0].childNodes[0].nodeValue;
				else
					cost = '';
				
				if (fooditem.getElementsByTagName('type')[0].childNodes.length > 0)
					var itemType = fooditem.getElementsByTagName('type')[0].childNodes[0].nodeValue;
				else
					itemType = '';
				
				html += addFoodItemRow(type, id, name, description, cost, itemType, available);
			}
			newIds = new Array(counter);
			for (var i = 0; i < counter; i++)
				newIds[i] = ids[i];
		}
		else {
			ids[loop] = id;
			
			// Get all the information from fooditem xml
			if (fooditem.getElementsByTagName('name')[0].childNodes.length > 0)
				name = fooditem.getElementsByTagName('name')[0].childNodes[0].nodeValue;
			else
				name = '';
			
			if (fooditem.getElementsByTagName('description')[0].childNodes.length > 0)
				description = fooditem.getElementsByTagName('description')[0].childNodes[0].nodeValue;
			else
				description = '';
			
			if (fooditem.getElementsByTagName('cost')[0].childNodes.length > 0)
				cost = fooditem.getElementsByTagName('cost')[0].childNodes[0].nodeValue;
			else
				cost = '';
			
			if (fooditem.getElementsByTagName('type')[0].childNodes.length > 0)
				itemType = fooditem.getElementsByTagName('type')[0].childNodes[0].nodeValue;
			else
				itemType = '';
			
			html += addFoodItemRow(type, id, name, description, cost, itemType, available);
		}
	}
	if (type == 'FoodItem')
		html +=
			'<input id="' + type +'Ids" value="' + newIds.join() +'" type="hidden" />' + 
			'</table>';
	else
		html +=
			'<input id="' + type +'Ids" value="' + ids.join() +'" type="hidden" />' + 
			'</table>';
	return html;
}

function addFoodItemRow(type, id, name, description, cost, itemType, available) {
	if (available == 'true')
		var availability = 'Yes';
	else
		availability = 'No';
	
	var	html =
		'<form id="list' + type + 'Form' + id + '">' +
		'<input id="name" value="' + name + '" type="hidden" />' +
		'<input id="description" value="' + description + '" type="hidden" />' +
		'<input id="cost" value="' + cost + '" type="hidden" />' +
		'<input id="itemType" value="' + itemType + '" type="hidden" />' +
		'<input id="available" value="' + available + '" type="hidden" />' +
		'</form>' +
		'<tr id="list' + type + 'Row' + id + '" class="searchResultsRow" onclick="set' + type + 'Id(\'' + id + '\'), select' + type + 'Row(\'' + id + '\')">' +
		'<td align="center">' + name + '</td>' +
		'<td align="center">' + itemType + '</td>' +
		'<td align="right">$' + parseFloat(cost).toFixed(2) + '</td>';
	if (type == 'FoodItem')
		html += '<td align="center">' + availability + '</td>';
	html += '</tr>';
	return html;
}

function selectFoodItemRow (id) {
	if (document.getElementById('FoodItemIds') != null) {
		var ids = document.getElementById('FoodItemIds').value.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listFoodItemRow' + ids[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listFoodItemRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}

function selectFoodItemInMIRow (id) {
	if (document.getElementById('FoodItemInMIIds') != null) {
		var ids = document.getElementById('FoodItemInMIIds').value.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			document.getElementById('listFoodItemInMIRow' + ids[loop]).className = 'searchResultsRow';
		}
		try {
			document.getElementById('listFoodItemInMIRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}


function setFoodItemId(id) {
	document.getElementById('foodItemID').value = id;
}

function setFoodItemInMIId(id) {
	document.getElementById('foodItemInMIID').value = id;
}